<template>
	<view class="container"></view>
</template>
<script>
	
	const TUICallEngine  = uni.requireNativePlugin('TencentCloud-TUICallKit-TUICallEngine'); //【1】import TUICallKit plugin
	const TUICallKit = uni.requireNativePlugin('TencentCloud-TUICallKit'); //【1】import TUICallKit plugin
	import { genTestUserSig } from '../../debug/GenerateTestUserSig.js'
	export default {
		name:'VideoChat',
		props:['callUserId','callUserName','avatarImg'],// callUserId 是对方的id
		data() {
			return {
				SDKAppID:'',
				userID:'100036723243'  // 这个是应用的userid
			}
		},
		methods: {
			// 调整渲染模式
			setCallMode(){
				const params = {
					userID: '234',
					fillMode: 1, // 0-填充模式，1一适应模式
					rotation: 1, // 0~3分别对应值：Rotation_0, Rotation_90, Rotation_180, Rotation_270;
				};
				TUICallEngine.setVideoRenderParams(params, (res) => {
					console.warn('res = ', JSON.stringify(res));
				});

			},
			callLogin() {
				const { userSig, sdkAppID: SDKAppID } = genTestUserSig(this.userID);
				const loginParams = { SDKAppID, userID: this.userID, userSig };   // apply SDKAppID、userSig
				
				// 设置用户昵称、头像
				const options = {
				  nickName: this.callUserName,
				  avatar: this.avatarImg
				}
				TUICallKit.setSelfInfo(options, (res) => {
				  if (res.code === 0) {
					console.log('setSelfInfo success');
				  } else {
					console.log(`setSelfInfo failed, error message = ${res.msg}`);
				  }
				});
				
				

				//【2】Login
				TUICallKit.login( loginParams, res => {
						this.tipMsg = res;
						if (res.code === 0) {
							this.SDKAppID = SDKAppID;
							console.log('[TUICallKit] login success.');
							this.handleCall();
						} else { 
                            console.error('[TUICallKit] login failed, failed message = ', res.msg, params);
						}
					}
				);	
			},
			handleCall() {
				try {
					const callParams = {
						userID: this.callUserId,
						callMediaType: 2   // 1 -- audio call，2 -- video call
					};
                    //【3】start 1v1 video call
					TUICallKit.call( callParams, res => {
							console.log('[TUICallKit] call params: ', JSON.stringify(res));
						}
					);
				} catch (error) {
					uni.$u.toast(JSON.stringify(error))
					// console.log('[TUICallKit] call error: ', error);
				}
			}
		}
	}
</script>
